from conf.readConfig import ReadConfig
import yagmail
import os

#实例化ReadConfig类
read_conf = ReadConfig()

#从配置文件中读取email各信息
mail_host = read_conf.get_mail("mail_host")
mail_user = read_conf.get_mail("mail_user")
mail_pass = read_conf.get_mail("mail_pass")
subject = read_conf.get_mail("subject")
sender = read_conf.get_mail("sender")
receiver = read_conf.get_mail("receiver")

#将收件人以逗号隔开生成列表，因为直接在yag.send中传入列表是不能发送的
receivers = receiver.split(",")

#从配置文件中读取存在报告的路径
report_dir = os.path.join(os.path.dirname(os.path.split(os.path.realpath(__file__))[0]),"report")
print(report_dir)
class SendMail():
    def send_mail(self):
        #将报告存放成列表，排序，读取读取倒数第二个也就是最近时间的邮件
        lists = os.listdir(report_dir)
        lists.sort()
        recent = lists[-2]

        file = os.path.join(report_dir,recent)
        yag = yagmail.SMTP(mail_user,mail_pass,mail_host)
        content ="""
        hi:
            接口自动化报告已生成
            请查收附件
            查看结果"""
        yag.send(receiver,"接口测试报告",content,file)

if __name__ == "__main__":
    SendMail().send_mail()
